package com.softme.trade.control.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.softme.trade.model.Role;
import com.softme.trade.model.dao.RoleDAO;
import com.softme.trade.util.BeanFactory;

@FacesConverter(value = "roleConverter", forClass = Role.class)
public class RoleConverter implements Converter
{

	private RoleDAO dao;

	@Override
	public Object getAsObject(FacesContext context, UIComponent comp, String value)
	{
		return this.getRoleDAO().findByName(value);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent comp, Object obj)
	{
		return ((Role)obj).getName();
	}

	private RoleDAO getRoleDAO()
	{
		if (this.dao == null || this.dao.getHibernateTemplate() == null)
			this.dao = (RoleDAO) BeanFactory.getBean("roleDAO", RoleDAO.class);

		return this.dao;
	}

}
